package org.csu.mypetstore.api.controller.front;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.AlipayConfig;
import org.csu.mypetstore.api.entity.Category;
import org.csu.mypetstore.api.entity.Order;
import org.csu.mypetstore.api.entity.Profile;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.service.OrderService;
import org.csu.mypetstore.api.vo.BillAddressVO;
import org.csu.mypetstore.api.vo.CartVO;
import org.csu.mypetstore.api.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/Order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CartService cartService;

    // 生成订单
    @PostMapping("newOrder")
    @ResponseBody
    CommonResponse<OrderVO> insertOrder(HttpSession session){
        return orderService.insertOrder(session);
    }

    //将前端的order的Bill或者全部数据存到session中
    @PostMapping("orderSession")
    @ResponseBody
    public CommonResponse<OrderVO> getCartSession(HttpSession session,Order order){
        return orderService.setCartSession(session,order);
    }

    //将前端的order的Ship数据存到session中
    @PostMapping("orderShipSession")
    @ResponseBody
    public CommonResponse<OrderVO> setCartShipSession(HttpSession session,Order order){
        return orderService.setCartShipSession(session,order);
    }

    //将session中的数据还给前端
    @GetMapping("oldOrder")
    @ResponseBody
    public CommonResponse<OrderVO> getCartSession(HttpSession session){
        return orderService.getCartSession(session);
    }

    //返回给前端已经加入数据库的数据
    @GetMapping("persistenceOrder")
    @ResponseBody
    public CommonResponse<OrderVO> selectOrder(HttpSession session){
        return orderService.selectOrder(session);
    }

    @GetMapping("pay/{totalPrice}/{orderId}/{orderName}/{orderDescrip}")
    @ResponseBody
    public CommonResponse<String> pay(@PathVariable(value="totalPrice")String totalPrice,
                      @PathVariable(value="orderId")String orderId,
                      @PathVariable(value="orderName")String orderName,
                      @PathVariable(value="orderDescrip")String orderDescrip,
                      Model model) throws UnsupportedEncodingException {
        System.out.println("*****************");
        System.out.println(totalPrice);
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = new String(orderId.getBytes("ISO-8859-1"),"UTF-8");
        //付款金额，必填
        String total_amount = new String(totalPrice.getBytes("ISO-8859-1"),"UTF-8");
        //订单名称，必填
        String subject = new String(orderName.getBytes("ISO-8859-1"),"UTF-8");
        //商品描述，可空
        String body = new String(orderDescrip.getBytes("ISO-8859-1"),"UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println("*****************########");
        System.out.println(result);

        //输出
        return CommonResponse.createForSuccess(result);
    }
}
